package cn.edu.cug.cs.gtl.oi.ui;

import cn.edu.cug.cs.gtl.oi.ui.actions.ProjectTreeModelListener;
import cn.edu.cug.cs.gtl.oi.ui.actions.ProjectTreeSelectionListener;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTree;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTreeNodeData;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTreeNodeEditor;
import cn.edu.cug.cs.gtl.oi.ui.projecttree.ProjectTreeNodeRenderer;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import java.awt.*;

public class ProjectPanel extends JPanel {

    MainApp theApp = null;
    ProjectTree treeComponent = null;

    public ProjectPanel(MainApp theApp,LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        this.theApp=theApp;
    }

    public ProjectPanel(MainApp theApp,LayoutManager layout) {
        super(layout);
        this.theApp=theApp;
    }

    public ProjectPanel(MainApp theApp,boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        this.theApp=theApp;
    }

    public ProjectPanel(MainApp theApp) {
        setLayout(new BorderLayout());
        this.theApp=theApp;
    }

    public ProjectTree getTreeComponent() {
        return treeComponent;
    }

    public void initial(){
        if(theApp.project==null) {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Project");
            treeComponent = new ProjectTree(root);

            final ProjectTreeNodeRenderer renderer = new ProjectTreeNodeRenderer();
            treeComponent.setCellRenderer(renderer);

            final ProjectTreeNodeEditor editor = new ProjectTreeNodeEditor(treeComponent);
            treeComponent.setCellEditor(editor);
            treeComponent.setEditable(true);

            JScrollPane scrollPane = new JScrollPane(treeComponent);
            add(scrollPane, BorderLayout.CENTER);
        }
        else {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(new ProjectTreeNodeData(theApp.getProject(),true));

            for(int i=0;i<this.theApp.getProject().getMapCount();++i){
                Map map = this.theApp.getProject().getMap(i);
                DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode(new ProjectTreeNodeData(map,true));
                for(int j=0;j<map.getLayerCount();++j){
                    Layer layer = map.getLayer(j);
                    mapNode.add(new DefaultMutableTreeNode(new ProjectTreeNodeData(layer,true)));
                }
                root.add(mapNode);
            }
            TreeModel treeModel = new DefaultTreeModel(root);
            treeComponent = new ProjectTree(treeModel);

            final ProjectTreeNodeRenderer renderer = new ProjectTreeNodeRenderer();
            treeComponent.setCellRenderer(renderer);

            final ProjectTreeNodeEditor editor = new ProjectTreeNodeEditor(treeComponent);
            treeComponent.setCellEditor(editor);
            treeComponent.setEditable(true);

            treeComponent.addTreeSelectionListener(new ProjectTreeSelectionListener(this.theApp));
            treeModel.addTreeModelListener(new ProjectTreeModelListener(this.theApp));

            JScrollPane scrollPane = new JScrollPane(treeComponent);
            add(scrollPane, BorderLayout.CENTER);
        }
    }
    public void reset(){
        if(treeComponent!=null)
            treeComponent.removeAll();
        if(theApp.project==null) {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Project");
            treeComponent.setModel(new DefaultTreeModel(root));
        }
        else {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(new ProjectTreeNodeData(theApp.getProject(),true));

            for(int i=0;i<this.theApp.getProject().getMapCount();++i){
                Map map = this.theApp.getProject().getMap(i);
                DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode(new ProjectTreeNodeData(map,true));
                for(int j=0;j<map.getLayerCount();++j){
                    Layer layer = map.getLayer(j);
                    mapNode.add(new DefaultMutableTreeNode(new ProjectTreeNodeData(layer,true)));
                }
                root.add(mapNode);
            }
            treeComponent.setModel(new DefaultTreeModel(root));
        }
    }
}
